﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using Microsoft.Xna.Framework;
using Tomahawk.Runtime.Logic.UI;
using Tomahawk.Runtime.Canvas.Sprite;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;

namespace Tomahawk.Runtime.Timeline
{
    public class RefractionEffectTrack : SpriteEffectTrack
    {
        [XmlElement("DisplacementX")]
        public string displacementX = "";

        [XmlElement("DisplacementY")]
        public string displacementY = "";

        private Vector2 displacement = Vector2.Zero;
        private Curve curveDisplacementX = new Curve();
        private Curve curveDisplacementY = new Curve();

        public RefractionEffectTrack() : base() { }

        public RefractionEffectTrack(string name) : base(name) { }

        protected override string GetEffectFile()
        {
            return @"Tomahawk.Content\SpriteEffects\RefractionEffect";
        }

        protected override void OnLoadEffectParameters(Effect effect)
        {
            effect.Parameters["DisplacementScroll"].SetValue(displacement);
        }

        protected override bool OnLoad(GraphicsDevice graphics, ContentManager contentManager)
        {
            curveDisplacementX = LoadCurve(contentManager, displacementX);
            curveDisplacementY = LoadCurve(contentManager, displacementY);

            return base.OnLoad(graphics, contentManager);
        }

        public override void SortKeys() { }

        public override bool Update(float time)
        {
            if (curveDisplacementX == null || curveDisplacementY == null)
            {
                return false;
            }

            displacement.X = curveDisplacementX.Evaluate(time);
            displacement.Y = curveDisplacementY.Evaluate(time);

            return true;
        }
    }
}
